************************************************** 
** Stata code to construct Table 3 in main text **
************************************************** 

global stataout [specify local folder for figures and tables]

run "prep3.do"

capture program drop get_coef
program define get_coef
	quietly {  
	 `1' , absorb($fe)  
	 scalar nobs = e(N) 
	 capture scalar rsq = e(r2_a)
	 capture scalar rsq_p = e(r2_p)
	 capture scalar chi2 = e(chi2)
	 unique gvkey if e(sample) == 1
	 scalar cmp = `r(unique)'
	 unique pdpass if e(sample)  == 1 
	 scalar pd = `r(unique)'
	 lincom CAFC_post * $gap_mean 
	 local est = r(estimate)
	 local change = (exp(`est')-1)*100
	 scalar perct = `change'
	 matrix coef = e(b) 
	}   
end 

capture program drop alt_cafc 
program define alt_cafc 
quietly	 {  
		use "CAFC_reg.dta", clear  
		preserve   
        `1'
		gen rate_pre = .
		gen rate_post = .
		foreach j in 1 2 4 5 6 7 8 9 10{
			replace rate_pre = _b[ct`j'] if circuit == `j'
			quietly lincom ct`j' + cafcct`j'   
			replace rate_post = `r(estimate)' if circuit == `j' 
        } 
		replace rate_pre = 0 if circuit == 3
		replace rate_post = _b[cafcct3] if circuit == 3
		replace rate_pre = _b[ct5] if circuit == 11
		replace rate_post = _b[cafcct11] if circuit == 11
		keep circuit rate_pre rate_post 
		duplicates drop 
		
		forvalues i = 1/11{
		gen ratepre`i' = rate_pre if circuit == `i'
		bysort ratepre`i': replace ratepre`i' = ratepre`i'[_n-1] if mi(ratepre`i')
		gen ratepost`i' = rate_post if circuit == `i' 
		bysort ratepost`i': replace ratepost`i' = ratepost`i'[_n-1] if mi(ratepost`i')
		}
		
		merge 1:1 circuit using "hm_dc_circuit.dta", nogen 
		forvalues i = 1/10{
		gen temppre`i' = ratepre`i' * pre`i'
		}
		gen temppre11 = temppre5 
		egen ratepre_wt = rowtotal(temppre1 temppre2 temppre3 temppre4 temppre5 temppre6 ///
		 temppre7 temppre8 temppre9 temppre10 temppre11)
		drop temppre* 
		forvalues i = 1/11{
		 gen temppost`i' = ratepost`i' * post`i'
		}
		egen ratepost_wt = rowtotal(temppost1 temppost2 temppost3 temppost4 temppost5 ///
		 temppost6 temppost7 temppost8 temppost9 temppost10 temppost11)
		drop temppost*
		drop pre* post*
		
		keep circuit ratepre_wt ratepost_wt
		sort circuit 
		replace ratepre_wt = ratepre_wt[5] if circuit == 11
		gen gap = ratepost_wt - ratepre_wt
		sum gap 
		global gap_mean = r(mean)
		tempfile temp
		save `temp', replace
		restore  

		use "Patreg.dta", clear
**# Bookmark #1
		preserve  
		merge m:1 circuit using `temp', update replace   
		replace CAFC_post = post * gap 
		get_coef "`2'" 
		restore 
	 }  
end

capture program drop tse_boot  
program tse_boot, eclass  
	quietly {  
		use "CAFC_reg.dta", clear  
		preserve  
		bsample, strata(ct_post)    
        `1'
		gen rate_pre = .
		gen rate_post = .
		foreach j in 1 2 4 5 6 7 8 9 10{
			replace rate_pre = _b[ct`j'] if circuit == `j'
			quietly lincom ct`j' + cafcct`j'   
			replace rate_post = `r(estimate)' if circuit == `j' 
        } 
		replace rate_pre = 0 if circuit == 3
		replace rate_post = _b[cafcct3] if circuit == 3
		replace rate_pre = _b[ct5] if circuit == 11
		replace rate_post = _b[cafcct11] if circuit == 11
		keep circuit rate_pre rate_post 
		
		duplicates drop 
		forvalues i = 1/11{
		gen ratepre`i' = rate_pre if circuit == `i'
		bysort ratepre`i': replace ratepre`i' = ratepre`i'[_n-1] if mi(ratepre`i')
		gen ratepost`i' = rate_post if circuit == `i' 
		bysort ratepost`i': replace ratepost`i' = ratepost`i'[_n-1] if mi(ratepost`i')
		}
		
		merge 1:1 circuit using "hm_dc_circuit.dta", nogen 
		forvalues i = 1/10{
		gen temppre`i' = ratepre`i' * pre`i'
		}
		gen temppre11 = temppre5 
		egen ratepre_wt = rowtotal(temppre1 temppre2 temppre3 temppre4 temppre5 temppre6 ///
		 temppre7 temppre8 temppre9 temppre10 temppre11)
		drop temppre* 
		forvalues i = 1/11{
		 gen temppost`i' = ratepost`i' * post`i'
		}
		egen ratepost_wt = rowtotal(temppost1 temppost2 temppost3 temppost4 temppost5 ///
		 temppost6 temppost7 temppost8 temppost9 temppost10 temppost11)
		drop temppost*
		drop pre* post*
		keep circuit ratepre_wt ratepost_wt
		sort circuit 
		replace ratepre_wt = ratepre_wt[5] if circuit == 11
		gen gap = ratepost_wt - ratepre_wt
		
		tempfile temp
		save `temp', replace
		restore  

		use "Patreg.dta", clear
		preserve  
		bsample if insample == 1, cluster(circuit gvkey)
		merge m:1 circuit using `temp', update replace 
		replace CAFC_post = gap * post
		 `2',  absorb($fe) 
		restore 
	 }  
end  

capture program drop reg_out 
program define reg_out 
cd "$stataout"
outreg2 using "`1'", word  nor2 noobs  /// 
      addtext("Technology-Year-Industry FE", "Yes", "Circuit FE", "Yes") ///
	addstat( "Patents", nobs, ///
	  "Companies", cmp, "R-squared", rsq, ///
	  "Economic effect", perct) ///
	 sortvar(_b_CAFC_post  _b_emp_ln _b_revt_per_inh _b_ppe_per_inh _b_xrd_per_inh ///
	 _b_tobin  _b_nclaims_ln    _b_nciting_inh ///
	  _b_cons) bdec(3) tdec(3)  ctitle("`2'")
matrix drop _all 
scalar drop _all 
end 

global reps 1000
global seed 202106

global ctrvar emp_ln ppe_per_inh revt_per_inh xrd_per_inh tobin ///
       nclaims_ln  nciting_inh       
global fe year##nclass_ocl##sic2 circuit 

use "Patreg.dta", clear
alt_cafc "xi: ologit outcome claims ass home_circuit appeal ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year if !mi(subcat) & !mi(circuit)  , robust" ///
 "reghdfe xi_ln CAFC_post $ctrvar if manuf  == 1" 
simulate _b, reps($reps) seed($seed): ///
tse_boot "xi: ologit outcome claims ass home_circuit appeal ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year if !mi(subcat) & !mi(circuit) , robust" ///
 "reghdfe xi_ln CAFC_post $ctrvar if manuf  == 1"
bstat, stat(coef)  
outreg2 using "$stataout\table3", word replace nor2 noobs  /// 
      addtext("Technology-Year-Industry FE", "Yes", "Circuit FE", "Yes") ///
	addstat( "Patents", nobs, ///
	"Businesses", pd,  "Companies", cmp, "R-squared", rsq, ///
	  "Economic effect", perct) ///
	  sortvar(_b_CAFC_post  _b_emp_ln _b_revt_per_inh _b_ppe_per_inh _b_xrd_per_inh ///
	 _b_tobin  _b_nclaims_ln   _b_nciting_inh ///
	  _b_cons)	bdec(3) tdec(3)  ctitle("Weighted index")
matrix drop _all 
scalar drop _all 

*Setting up programs
global seed 20220518
global ctrvar emp_ln ppe_per_inh revt_per_inh xrd_per_inh tobin ///
	   nclaims_ln  nciting_inh   
global fe year##nclass_ocl##sic2 circuit 
global reps 1000
 
preserve 
use "Patreg.dta", clear  
keep gap_all_home circuit pdpass year  
duplicates drop 
sum gap_all_home
global gap_mean = r(mean)
restore

capture program drop get_coef
program define get_coef
	quietly {  
	 `1' , absorb($fe)  
	 scalar nobs = e(N) 
	 capture scalar rsq = e(r2_a)
	 capture scalar rsq_p = e(r2_p)
	 capture scalar rsq_within = e(r2_a_within) 
	 capture scalar chi2 = e(chi2)
	 unique gvkey if e(sample) == 1
	 scalar cmp = `r(unique)'
	 lincom CAFC_home *$gap_mean 
	 local est = r(estimate)
	 local change = (exp(`est')-1)*100
	 scalar perct = `change'
	 matrix coef = e(b) 
	}   
end 
 
capture program drop reg_out 
program define reg_out 
cd "$stataout"
outreg2 using "`1'", word  nor2 noobs  /// 
      addtext("Technology-Year-Industry", "Yes", "Circuit FE", "Yes") ///
	addstat( "Companies", cmp, "Adjusted R-squared", rsq, ///
	"Within R-squared", rsq_within ,   "Economic effect", perct) ///
	 sortvar(_b_CAFC_home _b_CAFC_forshop  _b_emp_ln _b_revt_per_inh _b_ppe_per_inh ///
	 _b_nclaims _b_novelty_res_inh  _b_nciting_inh  ///
	  _b_cons) bdec(3) tdec(3)  ctitle("`2'")
matrix drop _all 
scalar drop _all 
end 


global sample1 if !mi(subcat) & !mi(circuit) 

capture program drop tse_boot  
program tse_boot, eclass  
	quietly {  
		use "CAFC_reg.dta", clear  
		preserve  
		bsample, strata(ct_post)    
		xi: ologit outcome patdef claims ass home_circuit appeal  ///
		ct1 ct2 ct4-ct10 cafcct1-cafcct11 hjt1-hjt5 i.year ///
		$sample1, robust

		foreach j in 1 2 4 5 6 7 8 9 10{
		replace ratepre_all = _b[ct`j'] if circuit == `j'
		quietly lincom ct`j' +   cafcct`j'   
		replace ratepost_all = `r(estimate)' if circuit == `j' 
		}

		replace ratepre_all = 0 if circuit == 3
		replace ratepost_all = _b[cafcct3] if circuit == 3
		replace ratepre_all = _b[ct5] if circuit == 11
		replace ratepost_all = _b[cafcct11] if circuit == 11
		
		gen gap_all = ratepost_all - ratepre_all 
		keep circuit gap_all 
		duplicates drop 
		tempfile temp
		save `temp', replace
		restore  

		use "Patreg.dta", clear
		preserve  
		bsample  , cluster(gvkey circuit)
		merge m:1 circuit using `temp',  update replace
		replace CAFC_home = gap_all * post * temp
		replace CAFC_forshop = gap_all * post * (1 - temp) 
		 `1',  absorb($fe) 
		restore 
	 }  
end  

use "Patreg.dta", clear
get_coef "reghdfe xi_ln CAFC_home CAFC_forshop $ctrvar if manuf  == 1"
simulate _b, reps($reps) seed($seed): ///
tse_boot "reghdfe xi_ln CAFC_home CAFC_forshop $ctrvar if manuf == 1"
bstat, stat(coef) 
cd "$stataout"
reg_out "table3" "Forum shopping" 
